<template>
  <div class="home-container">
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <h2 class="header-title">购物管理系统</h2>
      </el-header>

      <el-container>
        <!-- 侧边栏菜单 -->
        <el-aside width="200px">
          <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical-demo"
            @select="handleMenuSelect"
          >
            <el-menu-item index="/adminhomeproduct">
              <i class="el-icon-goods"></i>
              <span>商品管理</span>
            </el-menu-item>
            <el-menu-item index="/adminhomeorder">
              <i class="el-icon-s-order"></i>
              <span>订单管理</span>
            </el-menu-item>
            <el-menu-item index="/adminhomeuser">
              <i class="el-icon-user"></i>
              <span>用户管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <!-- 主内容区域 -->
        <el-main>
          <el-table :data="userData.data" style="width: 100%">
            <el-table-column width="40"></el-table-column>
            <el-table-column
              prop="id"
              label="用户id"
              width="180"
            ></el-table-column>
            <el-table-column
              prop="username"
              label="用户姓名"
              width="180"
            ></el-table-column>
            <el-table-column
              prop="email"
              label="用户邮箱"
              width="240"
            ></el-table-column>
            <el-table-column label="用户日志" width="600">
              <template #default="scope">
                <el-button size="mini" @click="GoToBrowseLogs(scope.row.id)">
                  浏览日志
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="GoToOrderLogs(scope.row.id)"
                >
                  购买日志
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      activeMenu: '/adminhomeuser', // 默认选中的菜单项
      userData: {}, // 用户数据
    }
  },
  mounted() {
    this.getUserData()
  },
  methods: {
    GoToBrowseLogs(id) {
      this.$router.push({ name: 'BrowseLog', params: { id } })
    },
    GoToOrderLogs(id) {
      this.$router.push({ name: 'OrderLog', params: { id } })
    },
    handleMenuSelect(index) {
      console.log(`选中菜单项: ${index}`)
      this.activeMenu = index
      // 跳转到对应的路由路径
      this.$router.push(index).catch((err) => {
        // 捕获错误，避免重复点击报错
        if (err.name !== 'NavigationDuplicated') {
          console.error(err)
        }
      })
    },
    //获取用户数据
    getUserData() {
      const token = localStorage.getItem('jwtToken') // 获取存储的 token

      this.$http
        .get('/api/adminhome/list', {
          headers: {
            Authorization: `Bearer ${token}`, // 使用 Bearer 方案
          },
        })
        .then((res) => {
          this.userData = res.data
          console.log(this.userData.data)
        })
        .catch((err) => {
          console.log(err)
        })
    },
  },
}
</script>

<style scoped>
.header-title {
  padding-left: 200px;
}
</style>